package com.medicine.hbp.ui;

import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.MyApplication;
import com.medicine.hbp.uitl.StartIntent;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.R;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * 消息界面
 *
 * @author Administrator
 */
public class NewsActivity extends BaseActivity {
	@BindView(id = R.id.btn_exit_app, click = true)
	public Button btn_exit_app;

	@BindView(id = R.id.layout_wx, click = true)
	public LinearLayout layout_wx;

	private IWXAPI api;

	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_news);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		api = WXAPIFactory.createWXAPI(this, "wx971b3c02475d90f2");
		initTop();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (LoginSp.getUserId(aty) != 0) {
			btn_exit_app.setVisibility(View.VISIBLE);
		} else {
			btn_exit_app.setVisibility(View.GONE);
		}
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.btn_exit_app:
				// Intent intent=new
				// Intent(NewsActivity.this,PayDemoActivity.class);
				// startActivity(intent);
				httpLogout();
				break;
			case R.id.layout_wx:
				httpWx01();
				break;
			default:
				break;
		}
	}

	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle("消息");
		topview.setLeftOnClick(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (LoginSp.getUserId(aty) != 0) {
					StartIntent.startUser(aty, LoginSp.getUserId(aty));
				} else {
					StartIntent.startLogin(aty);
				}
			}
		});
		topview.setRightOnClick(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				StartIntent.startRelease(aty);
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 退出登录
	 */
	private void httpLogout() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(this, HttpInterface.LOGIN_OUT, params,
				new AppHttpCallBack<Object>(Object.class, aty) {
					@Override
					public void onSuccess(Object result) {
						LoginSp.exitUser(aty);
						MyApplication.getMyApplication().setCookitString("");
						ToastUtil.showShort(aty, "退出系統成功");
						btn_exit_app.setVisibility(View.GONE);
						StartIntent.startLogin(aty);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}

	/**
	 * 退出登录
	 */
	private void httpWx01() {
		HttpParams params = new HttpParams();
		params.put("id", 0);
		AppHttps.HttpPost(this, HttpInterface.PAY_SIGN_WX, params,
				new AppHttpCallBack<Object>(Object.class, aty) {
					@Override
					public void onSuccess(String t) {
						// TODO Auto-generated method stub
						super.onSuccess(t);
						JSONObject json;
						try {
							json = new JSONObject(t);
							PayReq req = new PayReq();
							// req.appId = "wxf8b4f85f3a794e77"; // 测试用appId
							req.appId = json.getString("appid");
							req.partnerId = json.getString("partnerid");
							req.prepayId = json.getString("prepayid");
							req.nonceStr = json.getString("noncestr");
							req.timeStamp = json.getString("timestamp");
							req.packageValue = json.getString("package");
							req.sign = json.getString("sign");
							// req.extData = "app data"; // optional
							// 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
							api.sendReq(req);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						// ToastUtil.showShort(aty, errorMsg);
					}

					@Override
					public void onSuccess(Object result) {
					}
				});

	}

	/**
	 * 退出登录
	 */
	private void httpWx02() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(this, HttpInterface.FIND_SIGN_WX, params,
				new AppHttpCallBack<Object>(Object.class, aty) {
					@Override
					public void onSuccess(Object result) {
						LoginSp.exitUser(aty);
						MyApplication.getMyApplication().setCookitString("");
						ToastUtil.showShort(aty, "退出系統成功");
						btn_exit_app.setVisibility(View.GONE);
						StartIntent.startLogin(aty);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}
}
